package tt.dz.mvc.controller.base;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import tt.dz.dao.ext.station.OmsStationDao;
import tt.dz.dao.ext.system.OmsOperListDao;
import tt.dz.entity.base.JsonCommon;
import tt.dz.entity.base.JsonForList;
import tt.dz.entity.base.OmsEntityEnum;
import tt.dz.entity.station.OmsStation;
import tt.dz.entity.system.OmsOperList;
import tt.dz.entity.system.OmsOperStation;
import tt.dz.service.base.OperatorStationService;
import tt.dz.service.imp.base.BaseService;
import tt.dz.service.sys.StationService;
import tt.dz.util.ActionUtils;
import tt.dz.util.OmsConstant;
import tt.dz.util.StringUtil;
@Controller
@RequestMapping("operStationApi")
public class OperatorStationAction extends BaseAction
{
	@Autowired
	private OperatorStationService operatorStationServiceImpl;
	@Autowired
	private StationService stationService;
	@Autowired
	private OmsStationDao omsStationDao;
	@Autowired
	private OmsOperListDao operatorDaoImpl;
	
	//1 查询运营商所属站点列表
	@RequestMapping(value="findStationsByOperMerchantId",method=RequestMethod.POST)
	@ResponseBody
	public JsonForList<?> findStationsByOperMerchantId(HttpServletRequest request,HttpServletResponse response)
	{
//		List<OmsOperStation> oss = this.genarateBatchOos(request, true,false);
//		if (oss == null)
//		{
//			JsonCommon<String> errJc = new JsonCommon<>();
//			errJc.setCode(BaseService.MsgCodeEnum.ERROR400.getMsgCode());
//			errJc.setMsg("参数oper_id和station_id个数必须一致");
//			errJc.setPojo("error");
//			return errJc;
//		}
//		return operatorStationServiceImpl.addOperStations(oss);
		String oper_merchant_id = request.getParameter("oper_merchant_id");
		if(StringUtil.isBlank(oper_merchant_id)){
			return new JsonForList<>(null,OmsConstant.ErrorCodeEnum.MissParam.value(), OmsConstant.ErrorCodeEnum.MissParam.desc());
		}
		OmsStation query = new OmsStation();
		query.setOper_merchant_id(oper_merchant_id);
		return stationService.getStationList(query, null);
	}
	
	//2 查询运营商下属分公司管理员列表 
	@SuppressWarnings("unchecked")
	@RequestMapping(value="findOperListByOperMerchantId",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findOperListByOperMerchantId(HttpServletRequest request,HttpServletResponse response)
	{
		String oper_merchant_id = request.getParameter("oper_merchant_id");
		if(StringUtil.isBlank(oper_merchant_id)){
			return new JsonForList<>(null,OmsConstant.ErrorCodeEnum.MissParam.value(), OmsConstant.ErrorCodeEnum.MissParam.desc());
		}
		OmsOperList query = new OmsOperList();
//		query.setOper_merchant_id(oper_merchant_id);
		String condition = " where entity_type = 2 and entity_id in (select oper_merchant_branch_id from oms_operation_branch_sp_list where oper_merchant_id ='"+ oper_merchant_id+"')";
		return new JsonForList(operatorDaoImpl.findListByCondition(query, condition),OmsConstant.SuccessCodeEnum.SuccessCode.value(),OmsConstant.SuccessCodeEnum.SuccessCode.desc());
	}
	
	//3 管理员绑定站
	@RequestMapping(value="batchAdd",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> batchAdd(HttpServletRequest request,HttpServletResponse response)
	{
		List<OmsOperStation> oss = this.genarateBatchOos(request, true,false);
		return operatorStationServiceImpl.addOperStations(oss);
	}
	//4 解绑
	@RequestMapping(value="delete",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> delete(HttpServletRequest request,HttpServletResponse response)
	{
		List<OmsOperStation> oss = this.genarateBatchOos(request, false,false);
		return operatorStationServiceImpl.physicalDeleteOperStations(oss);
	}
	
	//5 查询运营商分公司管理员对应站点列表
	@RequestMapping(value="findStationsByOperId",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findStationsByOperId(HttpServletRequest request,HttpServletResponse response)
	{
		String oper_id = request.getParameter("oper_id");
		if(StringUtil.isBlank(oper_id)){
			return new JsonForList<>(null,OmsConstant.ErrorCodeEnum.MissParam.value(), OmsConstant.ErrorCodeEnum.MissParam.desc());
		}
//		OmsStation query = new OmsStation();
//		query.setOper_merchant_id(oper_merchant_id);
		//权限1 、2 需要分别处理
		//String condition =" where station_id in (select station_id from oms_oper_station where oper_id ='"+ oper_id +"' )";
		//return new JsonForList<>(omsStationDao.findListByCondition( new OmsStation(), condition),OmsConstant.SuccessCodeEnum.SuccessCode.value(),OmsConstant.SuccessCodeEnum.SuccessCode.desc());
		
		return operatorStationServiceImpl.getStationsByOperId(oper_id);

	}
	
	//6 查询运营商管理员对应未分配站点列表
	@RequestMapping(value="findUnallocatedStationsByOperId",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findUnallocatedStationsByOperId(HttpServletRequest request,HttpServletResponse response)
	{
		String oper_id = request.getParameter("oper_id");
		String entity_type = request.getParameter("entity_type");
		if(StringUtil.isBlank(oper_id) || StringUtil.isBlank(entity_type)){
			return new JsonForList<>(null,OmsConstant.ErrorCodeEnum.MissParam.value(), OmsConstant.ErrorCodeEnum.MissParam.desc());
		}
		return operatorStationServiceImpl.getUnallocatedStationsByOperId(oper_id, entity_type);
	}
	//6 查询运营商分公司管理员对应未分配站点列表
	@RequestMapping(value="findUnallocatedStationsForBranch",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findUnallocatedStationsForBranch(HttpServletRequest request,HttpServletResponse response)
	{
		String merchant_oper_id = request.getParameter("merchant_oper_id");
		String branch_oper_id = request.getParameter("branch_oper_id");
		String entity_type = request.getParameter("entity_type");
		if(StringUtil.isBlank(merchant_oper_id) || StringUtil.isBlank(entity_type)|| StringUtil.isBlank(branch_oper_id)){
			return new JsonForList<>(null,OmsConstant.ErrorCodeEnum.MissParam.value(), OmsConstant.ErrorCodeEnum.MissParam.desc());
		}
		return operatorStationServiceImpl.getUnallocatedStationsForBranch(merchant_oper_id,branch_oper_id, entity_type);
	}
	
	@RequestMapping(value="batchDelete",method=RequestMethod.POST)
	@ResponseBody
	public JsonCommon<?> batchDelete(HttpServletRequest request,HttpServletResponse response)
	{
		List<OmsOperStation> oss = this.genarateBatchOos(request, false,false);
		return operatorStationServiceImpl.deleteOperStations(oss);
	}
	

	
	@RequestMapping(value="findPagOrAll",method=RequestMethod.GET)
	@ResponseBody
	public JsonForList<?> findPagOrAll(HttpServletRequest request,HttpServletResponse response)
	{
		OmsOperList user = this.getLoginUser(request);
		OmsStation os = this.genarateObj(request, this.getLoginUserId(request), false,true);
		return operatorStationServiceImpl.findStationByUserId(user.getEntity_id(), 
				user.getIsSupper() == 1 ? OmsEntityEnum.SUPER_OPERMERCHANT.getValue()	: user.getEntity_type(), 
				this.buildQueryParam(request, true), os);
	}
	
	private OmsStation genarateObj(HttpServletRequest request,String userId,boolean isCreate,boolean isFind)
	{
		Map<String,String> params = ActionUtils.getParameterMap(request);
		return ActionUtils.getParamInstance(OmsStation.class, params, userId, isCreate,isFind);
	}
	
	private List<OmsOperStation> genarateBatchOos(HttpServletRequest request, boolean isCreate, boolean isFind) {
		OmsOperList user = this.getLoginUser(request);
		String operId = request.getParameter("operId");
		List<String> stationIds = JSON.parseArray(request.getParameter("stationIds"), String.class);
		List<OmsOperStation> oss = new ArrayList<OmsOperStation>();
		for (String stationId : stationIds) {
			OmsOperStation oos = new OmsOperStation();
			if (isCreate) {
				oos.setCreate_oper_id(user.getOper_id());
				oos.setValid_flag(1);
			} else {
				oos.setUpdate_oper_id(user.getOper_id());
			}
			oos.setOper_id(operId);
			oos.setStation_id(stationId);
			oss.add(oos);
		}
		return oss;
	}
}
